import GameManager from './managers/GameManager.js';
import UIManager from './managers/UIManager.js';
import AdaptManager from './managers/AdaptManager.js';
import Config from './utils/Config.js';

/**
 * 微信小游戏主入口类
 * 负责初始化游戏和协调各个管理器
 */
export default class Main {
    constructor() {
        this.adaptManager = null;
        this.gameManager = null;
        this.uiManager = null;
        this.init();
    }

    /**
     * 初始化微信小游戏
     */
    init() {
        console.log('微信小游戏初始化开始...');
        
        // 初始化配置
        Config.init();
        
        // 初始化适配管理器
        this.adaptManager = new AdaptManager();
        this.adaptManager.init();
        
        // 创建管理器实例
        this.uiManager = new UIManager();
        this.gameManager = new GameManager(this.uiManager);
        
        // 设置相互引用
        this.uiManager.setGameManager(this.gameManager);
        this.uiManager.setAdaptManager(this.adaptManager);
        
        // 启动游戏
        this.start();
    }

    /**
     * 启动游戏
     */
    start() {
        console.log('游戏启动...');

        // 初始化UI
        this.uiManager.init();

        // 启动游戏逻辑
        this.gameManager.start();

        console.log('游戏启动完成！');

        // 延迟执行坐标转换调试（等待所有组件初始化完成）
        setTimeout(() => {
            this.runCoordinateDebug();
        }, 1000);
    }

    /**
     * 运行坐标转换调试
     */
    runCoordinateDebug() {
        console.log('\n=== 自动坐标转换调试 ===');

        if (this.adaptManager) {
            // 显示适配信息
            this.adaptManager.showAdaptInfo();

            // 测试问题坐标点
            const problemPoints = [
                { x: 56.30, y: 112.11, desc: '卡牌图像点击（问题点）' },
                { x: 111.97, y: 111.08, desc: '卡牌边框点击（正常点）' }
            ];

            console.log('\n--- 问题坐标测试 ---');
            problemPoints.forEach(point => {
                console.log(`\n测试点: ${point.desc}`);

                const original = this.adaptManager.screenToGame(point.x, point.y);
                const fixed = this.adaptManager.screenToGameFixed(point.x, point.y);
                const simple = this.adaptManager.screenToGameSimple(point.x, point.y);

                console.log(`原始方法: (${point.x}, ${point.y}) -> (${original.x.toFixed(2)}, ${original.y.toFixed(2)})`);
                console.log(`修复方法: (${point.x}, ${point.y}) -> (${fixed.x.toFixed(2)}, ${fixed.y.toFixed(2)})`);
                console.log(`简化方法: (${point.x}, ${point.y}) -> (${simple.x.toFixed(2)}, ${simple.y.toFixed(2)})`);

                // 检查是否在备战区范围内
                const battleAreaRange = { x: [25, 120], y: [80, 750] };
                const inBattleArea = (coords) => {
                    return coords.x >= battleAreaRange.x[0] && coords.x <= battleAreaRange.x[1] &&
                           coords.y >= battleAreaRange.y[0] && coords.y <= battleAreaRange.y[1];
                };

                console.log(`原始方法在备战区内: ${inBattleArea(original)}`);
                console.log(`修复方法在备战区内: ${inBattleArea(fixed)}`);
                console.log(`简化方法在备战区内: ${inBattleArea(simple)}`);
            });

            // 暴露调试方法到全局
            window.adaptManager = this.adaptManager;
            window.uiManager = this.uiManager;
            console.log('\n调试对象已暴露到全局: window.adaptManager, window.uiManager');
            console.log('可以在控制台中调用 adaptManager.debugCoordinates() 进行更多测试');
        }

        console.log('=== 坐标转换调试完成 ===\n');
    }
}
